package org.ww.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.ww.commonutils.JwtUtils;
import org.ww.commonutils.MD5;
import org.ww.servicebase.exceptionhandler.GuliException;
import org.ww.ucenter.bean.Member;
import org.ww.ucenter.bean.vo.MemberVo;
import org.ww.ucenter.mapper.MemberMapper;
import org.ww.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author ww
 * @since 2020-11-02
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String login(Member member) {
        //获取手机号
        String phone=member.getMobile();
        //获取密码
        String password=member.getPassword();

        //首先判断用户名或密码是否为空
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"用户名或密码为空");
        }
        //根据手机号查询用户
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",phone);
        Member user = baseMapper.selectOne(wrapper);

        //判断密码是否正确
        if(!user.getPassword().equals(MD5.encrypt(password))){
            throw new GuliException(20001,"用户名或密码错误");
        }

        //判断该账户是否可用
        if(user.getIsDisabled()){
            throw new GuliException(20001,"该账号不可用");
        }
        //获取token
        String jwtToken = JwtUtils.getJwtToken(user.getId(), user.getNickname());
         return jwtToken;
    }

    /**
     * 用户注册接口
     * @param memberVo
     */
    @Override
    public void register(MemberVo memberVo) {

        //获取用户数据
        String code = memberVo.getCode();//验证码
        String mobile = memberVo.getMobile();//手机号
        String nickname = memberVo.getNickname();//昵称
        String password = memberVo.getPassword();//密码

        //判断昵称是否为空
        if(StringUtils.isEmpty(nickname)){
            throw new GuliException(20001,"昵称为空");
        }

        //判断用户名和密码是否为空
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"用户名或密码为空");
        }

        //判断验证码是否为空
        if(StringUtils.isEmpty(code)){
            throw new GuliException(20001,"验证码为空");
        }

        //判断验证码是否正确
        String redisCode = (String) redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new GuliException(20001,"验证码不正确");
        }

        //判断该用户是否已经存在
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            throw new GuliException(20001,"该用户已经存在");
        }
        //添加用户
        Member m=new Member();
        m.setNickname(nickname);
        m.setMobile(mobile);
        m.setPassword(MD5.encrypt(password));
        m.setIsDisabled(false);
        m.setAvatar("https://ww-edu.oss-cn-beijing.aliyuncs.com/2020/10/203350978b85204c2b8ca67c788882f3d2file.png");

        baseMapper.insert(m);

    }

    /**
     * 根据Token获取用户信息
     * @param request
     * @return
     */
    @Override
    public Member getUserInfo(HttpServletRequest request) {
        //获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(memberId==null){
            throw new GuliException(20001,"请先进行登录!!!");
        }
        //根据id获取用户信息
        return baseMapper.selectOne(new QueryWrapper<Member>().eq("id",memberId));
    }

    /**
     * 根据openid获取用户信息
     * @param openid
     * @return
     */
    @Override
    public Member getByOpenid(String openid) {
        return baseMapper.selectOne(new QueryWrapper<Member>().eq("openid",openid));
    }

    /**
     * 根据日期得到该日注册的人数
     * @param date
     * @return
     */
    @Override
    public Integer getRegisterMemberCount(String date) {
        return baseMapper.getRegisterMemberCount(date);
    }
}
